import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:live_music/components/feedback/PageFeedBack.dart';
import 'package:live_music/components/index/momentCard.dart';
import 'package:live_music/components/ui/theme.dart';
import 'package:live_music/models/notification_detail_model.dart';
import 'package:live_music/models/user_info_model.dart';
import 'package:live_music/pages/friendList/widget/list_card_friend.dart';
import 'package:live_music/pages/home/message/message_page/atAndCommentInfo_card.dart';
import 'package:live_music/pages/home/message/message_page/follow_card.dart';
import 'package:live_music/services/notificationService.dart';
import 'package:live_music/services/userService.dart';
import 'package:live_music/utils/colors.dart';

class FollowIndexPage extends StatefulWidget {
  FollowIndexPage({Key key}) : super(key: key);

  @override
  _FollowIndexPageState createState() => _FollowIndexPageState();
}

class _FollowIndexPageState extends State<FollowIndexPage>
    with AutomaticKeepAliveClientMixin {
  // 下拉刷新控制器
  EasyRefreshController _easyRefreshController;
  // 独立的滚动视图 防止tab切换时触发下拉刷新
  ScrollController _scrollController;
  // 列表数组模型
  List<NotificationDetailModel> _notificationListModel;
  int pageNo = 1;
  int pageSize = 20;
  bool hasMore = true;
  bool loading = true;
  bool error = false;
  String errorMsg;

  @override
  void initState() {
    super.initState();
    _easyRefreshController = EasyRefreshController();
    _scrollController = ScrollController();
    getData();
  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    _scrollController.dispose();
    super.dispose();
  }

  getData({bool replace = true}) async {
    try {
      Map<String, dynamic> data = {"pageNo": pageNo, "pageSize": pageSize};
       Map<String, dynamic> result = await NotificationService.queryFollowInfo(data);
      List<dynamic>  notificationList = result['body'];
      NotificationListModel notificationListModel = NotificationListModel.fromJson( notificationList);
      print(notificationListModel);
      setState(() {
        pageNo++;
        hasMore = pageSize == notificationListModel.list.length;
        if (replace) {
          _notificationListModel = notificationListModel.list;
        } else {
         _notificationListModel.addAll(notificationListModel.list);
        }
      });
    } catch (e) {
      setState(() {
        error = true;
        errorMsg = e.toString();
      });
    } finally {
      setState(() {
        loading = false;
      });
    }
  }

  // 刷新回调(null为不开启刷新)
  Future _onRefresh() async {
    setState(() {
      pageNo = 1;
    });
    await getData(replace: true);
    if (error) {
      setState(() => error = false);
    }
    // 恢复刷新状态 使onLoad再次可用
    _easyRefreshController.resetLoadState();
  }

  // 加载回调(null为不开启加载)
  Future _onLoad() async {
    if (hasMore) {
      await getData(replace: false);
    }
    // // 结束加载
    
    // _easyRefreshController.finishLoad(success: true, noMore: !hasMore);
     await Future.delayed(const Duration(seconds: 0), () {
      _easyRefreshController.finishLoad(success: true, noMore: !hasMore);
    });
  }
  @override
  Widget build(BuildContext context) {
    super.build(context);
    double screeHeight = MediaQuery.of(context).size.height;
    return Scaffold(
        backgroundColor: ColorRes.themeBg,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: Text(
            '新增关注',
            style: TextStyle(color: Colors.black, fontSize: 16),
          ),
          centerTitle: true,
        ),
        body:Column(children: [
      Container(
          height:screeHeight- 101,
          child: EasyRefresh(
              firstRefresh: false,
              firstRefreshWidget: PageFeedBack(firstRefresh: true).build(),
              emptyWidget: PageFeedBack(
                loading: loading,
                error: error,
                empty: _notificationListModel.isEmpty,
                errorMsg: errorMsg,
                onErrorTap: () => _easyRefreshController.callRefresh(),
                onEmptyTap: () => _easyRefreshController.callRefresh(),
              ).build(),
              header: ClassicalHeader(),
              footer: ClassicalFooter(),
              controller: _easyRefreshController,
              enableControlFinishRefresh: false,
              enableControlFinishLoad: true,
              onRefresh: _onRefresh,
              onLoad:_notificationListModel.isEmpty ? null : _onLoad,
              child: ListView.builder(
                  controller: _scrollController,
                  itemCount:_notificationListModel.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Container(
                        color: Colors.white, child: FollowCard( notificationItem:_notificationListModel[index], index: index,addFollowed:addFollowed));
                  })))
    ]));
  }
  addFollowed(hasFollowed, index) {
    setState(() {
   _notificationListModel[index].moment.hasFollowed = hasFollowed;
    });
  }
 

  @override
  bool get wantKeepAlive => true;
}
